package com.onedec.attendance.service.impl;

import com.onedec.attendance.dao.OnedecCheckingInDao;
import com.onedec.attendance.entity.OnedecCheckingIn;
import com.onedec.attendance.service.CheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckinServiceImpl implements CheckinService {
    @Resource
    private OnedecCheckingInDao checkingInDao;


    @Override
    public int insertNewCheckin(OnedecCheckingIn checkingIn) throws Exception {
        return checkingInDao.insert(checkingIn);
    }

    @Override
    public List<OnedecCheckingIn> selectAll() throws Exception {
        return checkingInDao.selectByExample(null);
    }

    @Override
    public int selectCheckINCount() throws Exception {
        return (int) checkingInDao.countByExample(null);
    }

    @Override
    public List<Map<String, Object>> selectAllCheckin(HashMap<String, Object> map) throws Exception {
        List<Map<String, Object>> list = checkingInDao.selectCheckIns(map);
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        // 修改时间格式
        for (Map<String, Object> exam : list) {
            if (exam.get("in_morning_closing_time") != null) {

                String temp = df.format(exam.get("in_morning_closing_time"));
                Date in_morning_closing_time = df.parse(temp);
                exam.put("in_morning_closing_time", df.format(in_morning_closing_time));
            }
            if (exam.get("in_morning_hours") != null) {

                String temp = df.format(exam.get("in_morning_hours"));
                Date in_morning_hours = df.parse(temp);
                exam.put("in_morning_hours", df.format(in_morning_hours));
            }
            if (exam.get("in_afternoon_hours") != null) {

                String temp = df.format(exam.get("in_afternoon_hours"));
                Date in_afternoon_hours = df.parse(temp);
                exam.put("in_afternoon_hours", df.format(in_afternoon_hours));
            }
            if (exam.get("in_afternoon_closing_time") != null) {

                String temp = df.format(exam.get("in_afternoon_closing_time"));
                Date in_afternoon_closing_time = df.parse(temp);
                exam.put("in_afternoon_closing_time", df.format(in_afternoon_closing_time));
            }

//            if (exam.get("in_afternoon_hours") != null) {
//                java.util.Date timeDate2 = df.parse(exam.get("in_afternoon_hours") + "");
//                java.sql.Date dateTime2 = new java.sql.Date(timeDate2.getTime());
//                exam.put("in_afternoon_hours", df.format(dateTime2));
//            }
        }
        return list;
    }

    @Override
    public Integer deleteCheckIn(Integer attid) throws Exception {
        int key = checkingInDao.deleteByPrimaryKey(attid);
        return key;
    }
}
